/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.service;

import com.yunqian.redot.domain.view.CoursesWordsDetails;
import com.yunqian.redot.repository.CoursesWordsDetailsRepository;
import com.yunqian.service.BaseService;
import com.yunqian.web.FilterCondition;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Map;

/**
 * @author czy
 * 2018-10-08 15:30:48
 */
@Log4j2
@Service
public class CoursesWordsDetailsService extends BaseService<CoursesWordsDetails, Long> {

    @Autowired
    private CoursesWordsDetailsRepository coursesWordsDetailsRepository;
    @Override
    public Page<CoursesWordsDetails> pagination(Map<String, Object> params) {
        Sort sort = new Sort(Sort.Direction.ASC, "asBrilliant")
                .and(new Sort(Sort.Direction.DESC, "saveTime"));
        Pageable pageable = this.getPageable(params, sort);
        Specification<CoursesWordsDetails> spec = (Specification<CoursesWordsDetails>) (root, query, criteriaBuilder) -> {
            FilterCondition filterCondition = new FilterCondition(criteriaBuilder, root, params);
            List<Predicate> list = filterCondition.getPredicateList();
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
        return coursesWordsDetailsRepository.findAll(spec, pageable);
    }
}
